/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 * 
 */

var email;
var name;
var boolLoadGame = true;

$(document).ready(function() {
    /* zebra stripe the tables (not necessary for scrolling) */
    var tbl = $("table.tbl1");
    addZebraStripe(tbl);
    addMouseOver(tbl);
			
    /* make the table scrollable with a fixed header */
    $("table.scroll").createScrollableTable({
        width: '450px',
        height: '250px'
    });

});

function addZebraStripe(table) {
    table.find("tbody tr:odd").addClass("alt");
}

function addMouseOver(table) {
    table.find("tbody tr").hover(
        function() {
            $(this).addClass("over");
        },
        function() {
            $(this).removeClass("over");
        }
        );
}

function init(emailParam, nameParam, creationErrorParam){
    
    email = emailParam;
    name= nameParam;
    creationError = creationErrorParam;
    if (email != "Guest"){
        div = document.getElementById("createTableDiv");
        
        div.appendChild(document.createTextNode(creationErrorParam));
        form = document.createElement("form");
        form.setAttribute("id", "createTableForm");
        form.setAttribute("method", "post");
        form.setAttribute("action", "?req=createGameTable");
        table = document.createElement("table");
        tr1 = document.createElement("tr");
        
        td1 = document.createElement("td");
        label = document.createTextNode("name");
        td1.appendChild(label);
        
        td2 = document.createElement("td");
        input = document.createElement("input");
        input.setAttribute("name","gameTableName");
        td2.appendChild(input);
        
        
        tr1.appendChild(td1);
        tr1.appendChild(td2);
        
        
        tr2 = document.createElement("tr");
        td3 = document.createElement("td");
        td3.appendChild(document.createTextNode(" "));
        
        
        td4 = document.createElement("td");
        
        submit = document.createElement("input");
        submit.setAttribute("type", "submit");
        submit.setAttribute("value", "Create");
        td4.appendChild(submit);
        
        
        tr2.appendChild(td3);
        tr2.appendChild(td4);
        
        table.appendChild(tr1);
        table.appendChild(tr2);
        form.appendChild(table);
        div.appendChild(form);
        
        
        
        setInterval(function(){
            ajaxRequest(onGameRecieved, "home.html?req=getGameTable");
        },4500);
        

        
        div = document.getElementById("authentication");
        
        
        var anchor = document.createElement("a");
        anchor.setAttribute("href", "edit_profile.html");
        anchor.setAttribute("style", "margin-right:12px");
        anchor.innerHTML="Edit Profile";
        div.appendChild(anchor);
        
        anchor = document.createElement("a");
        anchor.setAttribute("href", "?req=logout");
        anchor.setAttribute("style", "margin-left:12px");
        anchor.innerHTML="logout";
        div.appendChild(anchor);
        
        
        
    }
    if (email=="Guest"){
        
        div = document.getElementById("authentication");
        
        
        var anchor = document.createElement("a");
        anchor.setAttribute("href", "login.html");
        anchor.setAttribute("style", "margin-right:12px");
        anchor.innerHTML="Login";
        div.appendChild(anchor);
        
        anchor = document.createElement("a");
        anchor.setAttribute("href", "register.html");
        anchor.setAttribute("style", "margin-left:12px");
        anchor.innerHTML="Register";
        div.appendChild(anchor);
        
        
    }
    
    
    
    ajaxRequest(updateTableCallback, "?req=updateTable");
    setInterval(function(){
        ajaxRequest(updateTableCallback, "?req=updateTable");
    },4500);
    
    
    ajaxRequest(fillHighRankingsTable, "?req=getHighRankings");
    
}

function fillHighRankingsTable(data){
    var users = JSON.parse(data);
    table = document.getElementById("highRankingsTable");
    
    for (var i = 0 ; i < users.length ; i++){
        var tr = document.createElement("tr");
        
        var tdEmail = document.createElement("td");
        var tdFirstName = document.createElement("td");
        var tdLastName = document.createElement("td");
        var tdRating = document.createElement("td");
        
        tdEmail.appendChild(document.createTextNode(users[i].email));
        tdFirstName.appendChild(document.createTextNode(users[i].firstName));
        tdLastName.appendChild(document.createTextNode(users[i].lastName));
        tdRating.appendChild(document.createTextNode(users[i].rating));
        
        tr.appendChild(tdEmail);
        tr.appendChild(tdFirstName);
        tr.appendChild(tdLastName);
        tr.appendChild(tdRating);
        
        table.appendChild(tr);
    }
}

function onGameRecieved(data){
    if (data!="\n"){
        game = JSON.parse(data);
        if (game!=null){
            if (game!=""){
                if (!game.gameFinished)
                    if (game.player2!= null && boolLoadGame){
                        boolLoadGame = confirm("Player 2 joined, do you want to load the game?");
                        if (boolLoadGame)
                            ajaxRequest(loadGame, "?req=loadGame");
                    }
            }
        }
    }
}

function loadGame(data){
    window.location.href=data;
}


function updateTableCallback(data){
    if (data!=""){
        if (data!=undefined){
            var gameTables = JSON.parse(data);
            var theTable = document.getElementById("gameTables");
            //theTable.childNodes = undefined;
            rowCount = theTable.rows.length;
        
            for (i = rowCount-1 ; i>=0 ; i--){
                theTable.removeChild(theTable.rows[i]);
            }
            for (i = 0; i <gameTables.length ; i++ )
            {
                if (gameTables[i].gameFinished)
                    continue;
                var tableRow = document.createElement("tr");
                var tableData1 = document.createElement("td");
                var tableData2 = document.createElement("td");
                var tableData3 = document.createElement("td");

                            
                tableData1.appendChild(document.createTextNode(gameTables[i].name));
                tableData2.appendChild(document.createTextNode(gameTables[i].player1.name+" : "+gameTables[i].player1.rating));
                if (gameTables[i].player2== undefined && gameTables[i].player1.email!= email ){
                           
                    //tableData3.appendChild(document.createTextNode("join"));
                    var anchor = document.createElement("a");
                    anchor.innerHTML = "Join";
                    tableData3.appendChild(anchor);
                    anchor.setAttribute("href", "?req=joinTable&gameTableName="+ gameTables[i].name);

                }
                            
                else if (gameTables[i].player2!= undefined)
                    tableData3.appendChild(document.createTextNode(gameTables[i].player2.name+" : "+gameTables[i].player2.rating));
                        
                        
                           
                           
                tableRow.appendChild(tableData1);
                        
                tableRow.appendChild(tableData2);

                tableRow.appendChild(tableData3);
                      
                            

                        
                theTable.appendChild(tableRow);
                        


                            
                            
            
            }
        //        if (gameTables.length < 5 ){
        //            for (var i = 0 ; i<5-gameTables.length ; i++){
        //                tr = document.createElement("tr");
        //                td1 = document.createElement("td");
        //                td1.innerHTML="<p>&nbsp;</p>";
        //                td2 = document.createElement("td");
        //                td2.innerHTML="<p>&nbsp;</p>";
        //                td3 = document.createElement("td");
        //                td3.innerHTML="<p>&nbsp;</p>";
        //                    
        //                tr.appendChild(td1);
        //                tr.appendChild(td2);
        //                tr.appendChild(td3);
        //                theTable.appendChild(tr);
        //            }
        //        }
        
        
        }
    }
            
}
            
function createGameTable(){
    var request = "?req=createGameTable&gameTableName="+document.getElementById("gameTableName").value;
    ajaxRequest(createGameTableCallback,request )
                
}
            
function createGameTableCallback(data){
    document.getElementById("gameTableCreationStatus").innerHTML = data;
    ajaxRequest(updateTableCallback, "?req=updateTable");
}
            
            
